<template>
  <div v-if="timeType == 1">
    <div :class="[classObject.padding, classObject.bd]" class="c-fs22 c-fc-xblack c-fw400 c-flex-row c-aligni-center ">距结束
      <div :class="[classObject.color, classObject.bg, classObject.size]" class="c-br4 c-flex-row c-justify-center c-aligni-center c-mh8">{{d}}</div>天
      <div :class="[classObject.color, classObject.bg, classObject.size]" class="c-br4 c-flex-row c-justify-center c-aligni-center c-mh8">{{h}}</div>时
      <div :class="[classObject.color, classObject.bg, classObject.size]" class="c-br4 c-flex-row c-justify-center c-aligni-center c-mh8">{{m}}</div>分
      <div :class="[classObject.color, classObject.bg, classObject.size]" class="c-br4 c-flex-row c-justify-center c-aligni-center c-mh8">{{s}}</div>秒
    </div>
  </div>
  <div v-else>
    <p>距结束</p>
    <p>{{d}}天{{h}}:{{m}}:{{s}}</p>
  </div>
</template>

<script>
// let timeFunction = null;
export default {
  name: 'BargainTime',
  props: {
    time: {
      type: String,
      default: ''
    },
    classObject: {
      type: Object,
      default: () => {
        return {
          color: 'c-fc-white',
          bg: 'c-bg-sblack ',
          size: 'c-ww32 c-hh32',
          padding: 'c-pb10',
          bd: 'c-bd-b1'
        }
      }
    },
    timeType: {
      type: Number,
      default: 1
    },
  },
  data() {
    return {
      d: '',
      h: '', //时
      m: '', //分
      s: '', //秒: value
      hm: '', //毫秒
    }
  },
  deactivated() {
    clearInterval(this.timeFunction);
  },
  mounted() {
    clearInterval(this.timeFunction);
    this.timeFunction = setInterval(() => {
      this.timeTo(this.time.replace(/\-/g, "/"))
    })
  },
  methods: {
    timeTo(time) {
      let t = new Date(time).getTime(); //取得指定时间得总毫秒数
      let n = new Date().getTime(); //取得当前毫秒数
      let c = t - n; //时间差
      if (c <= 0) {
        clearInterval(this.timeFunction);
        this.d = 0;
        this.h = 0;
        this.m = 0;
        this.s = 0;
        this.hm = 0;
        this.$emit('finshTime');
        return
      }
      let d = parseInt(c / 1000 / 60 / 60 / 24);
      let h = parseInt(c / 1000 / 60 / 60 - (24 * d));
      let m = parseInt(c / 1000 / 60 - (24 * 60 * d) - (60 * h));
      let s = parseInt(c / 1000 - (24 * 60 * 60 * d) - (60 * 60 * h) - (60 * m));
      let hm = Math.floor((c - (24 * 60 * 60 * 1000 * d) - (60 * 60 * 1000 * h) - (60 * 1000 * m) - (s * 1000)) / 10);
      if (hm < 10) {
        hm = "0" + hm;
      }
      if (s < 10) {
        s = "0" + s;
      }
      if (m < 10) {
        m = "0" + m;
      }
      if (h < 10) {
        h = "0" + h;
      }
      this.d = d;
      this.h = h;
      this.m = m;
      this.s = s;
      this.hm = hm;
    },
  }
}
</script>

<style lang="less" scoped>
.c-mh8 {
  margin-left: 0.2rem;
  margin-right: 0.2rem;
}
</style>